package com.cvs.contorller;

 import com.cvs.bean.Jifen;
 import com.cvs.bean.JifenGood;
 import com.cvs.bean.Orders;
 import com.cvs.bean.User;
import com.cvs.service.JifenGoodService;
 import com.cvs.service.JifenService;
 import com.cvs.service.OrdersService;
 import com.cvs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/jifen")
public class JifenGoodController {
    @Autowired
    private UserService userService;
    @Autowired
    private JifenGoodService jifenGoodService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private JifenService jifenService;
    @RequestMapping("/index")
    public String jifenGood(int uid, Model model,int pageNum,int pageSize){
        User user=userService.findOne(uid);
        model.addAttribute("user",user);
       // List<JifenGood> list=jifenGoodService.findAll();
        model.addAttribute("goods",jifenGoodService.findAllBypage(pageNum,pageSize));
        return "JifenGood";
    }

    @RequestMapping ("/jifenGoods")
    public @ResponseBody Map jifenGoods(int uid,int pageNum,int pageSize){
        Map map=new HashMap();
        map.put("status",200);
        map.put("msg","查询成功");
        map.put("data",jifenGoodService.findAllBypage(pageNum,pageSize));
        return map;
    }
    @RequestMapping("/jifenGoodinfo")
    public String jifenGoodinfo(int uid,int id, Model model){
        User user=userService.findOne(uid);
        model.addAttribute("user",user);
        model.addAttribute("goodinfo",jifenGoodService.findOne(id));
        return "jifenGoodinfo";
    }
    @RequestMapping("/jifenBuy")
    public String jifenBuy(int uid,int id, Model model){
        User user=userService.findUserInfo(uid);
        model.addAttribute("user",user);

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyyMMddHHmmss");

        model.addAttribute("createtime", simpleDateFormat.format(new Date()));
        model.addAttribute("orderNum", simpleDateFormat1.format(new Date()));
        model.addAttribute("goodinfo",jifenGoodService.findGoodInfo(id));
        return "jifenBuy";
    }
    @RequestMapping("/makeOrder")
    public String makeOrder(int uid,int id, Model model,String orderNo){
        User user=userService.findOne(uid);
        JifenGood jifenGood=jifenGoodService.findOne(id);
        if(user.getJifen()<jifenGood.getMoney()){
            return "fail";
        }
        model.addAttribute("user",user);
        Orders orders=new Orders();
        orders.setUid(uid);
        orders.setGid(id);
        orders.setCount(1);
        orders.setOrderno(orderNo);
        orders.setOrdertime(new Date());
        orders.setStatues(1);
        //下单支付
      int flag= ordersService.insertOrder(orders);
      //购买后剩余积分
      int leftjf=user.getJifen()-jifenGood.getMoney();
      user.setJifen(leftjf);
      userService.updateUser(user);
      //增加积分记录
    Jifen jifen=new Jifen();
    jifen.setCreatetime(new Date());
    jifen.setUid(uid);
    jifen.setMoney(jifenGood.getMoney());
      jifenService.addJifenlog(jifen);
      //减少可兑换商品数量
        jifenGood.setUsercount(jifenGood.getUsercount()+1);
      jifenGoodService.updateJifenGood(jifenGood);
      model.addAttribute("goodinfo",jifenGood);
        model.addAttribute("orders",orders);
        return "orders";
    }
}
